package com.yao.sys.controller;

import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yao.common.bean.PageQueryBean;
import com.yao.common.helper.ValidateHelper;
import com.yao.common.resp.ApiResponse;
import com.yao.ro.entity.vo.GoodsVo;
import com.yao.sys.entity.dto.DictTypeDto;
import com.yao.sys.service.DictTypeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 字典类型表 Controller
 *
 * @author autoCode
 * @version 1.0.0-SNAPSHOT
 * @date 2020-04-09
 */
@Api(tags = "字典类型管理")
@RestController
@RequestMapping(value = "/dictType")
@Slf4j
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    @ApiOperation(value = "新增字典类型", notes = "新增字典类型")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponse<Object> add(@RequestBody DictTypeDto model) {

        ValidateHelper.validateNull(model, new String[] {"name", "code"});

        dictTypeService.add(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "修改字典类型", notes = "修改字典类型")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponse<Object> update(@RequestBody DictTypeDto model) {

        ValidateHelper.validateNull(model, new String[] {"id", "name", "code"});

        dictTypeService.modifyById(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "删除字典类型", notes = "逻辑删除字典类型")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ApiResponse<Object> deleteById(@PathVariable("id") Integer id) {

        ValidateHelper.validateId(id);

        dictTypeService.removeById(id);

        return ApiResponse.success();
    }

    @ApiOperation(value = "字典类型列表(分页)", notes = "字典类型列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponse<IPage<GoodsVo>> list(@RequestBody PageQueryBean<DictTypeDto> queryBean) {

        if (Objects.isNull(queryBean.getData())) {
            queryBean.setData(new DictTypeDto());
        }

        Page<DictTypeDto> page = dictTypeService.page(new Page<>(queryBean.getCurrent(), queryBean.getSize()),
            Wrappers.lambdaQuery(queryBean.getData()));

        return ApiResponse.success(page);
    }

}